
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 05/09/2012 13:50:40
-- Generated from EDMX file: C:\Users\Marek\Documents\Visual Studio 2010\Projects\IronBrokers.ClientManager\IronBrokers.ClientManager\Database\Entities.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [IronBrokers];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_IndexGood]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Goods] DROP CONSTRAINT [FK_IndexGood];
GO
IF OBJECT_ID(N'[dbo].[FK_GoodQuotation]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Quotations] DROP CONSTRAINT [FK_GoodQuotation];
GO
IF OBJECT_ID(N'[dbo].[FK_UserPriviledge]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Priviledges] DROP CONSTRAINT [FK_UserPriviledge];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Indexes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Indexes];
GO
IF OBJECT_ID(N'[dbo].[Goods]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Goods];
GO
IF OBJECT_ID(N'[dbo].[Quotations]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Quotations];
GO
IF OBJECT_ID(N'[dbo].[Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Users];
GO
IF OBJECT_ID(N'[dbo].[Priviledges]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Priviledges];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Indexes'
CREATE TABLE [dbo].[Indexes] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Identity] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Goods'
CREATE TABLE [dbo].[Goods] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Identity] nvarchar(max)  NOT NULL,
    [Index_Id] int  NOT NULL
);
GO

-- Creating table 'Quotations'
CREATE TABLE [dbo].[Quotations] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Timestamp] datetime  NOT NULL,
    [Value] decimal(18,0)  NOT NULL,
    [Good_Id] int  NOT NULL
);
GO

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [PasswdSH1] nvarchar(max)  NOT NULL,
    [IsSubscribed] bit  NOT NULL,
    [LastSubscription] datetime  NOT NULL,
    [TicketID] uniqueidentifier  NULL
);
GO

-- Creating table 'Priviledges'
CREATE TABLE [dbo].[Priviledges] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Topic] nvarchar(max)  NOT NULL,
    [User_Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Indexes'
ALTER TABLE [dbo].[Indexes]
ADD CONSTRAINT [PK_Indexes]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Goods'
ALTER TABLE [dbo].[Goods]
ADD CONSTRAINT [PK_Goods]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Quotations'
ALTER TABLE [dbo].[Quotations]
ADD CONSTRAINT [PK_Quotations]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Priviledges'
ALTER TABLE [dbo].[Priviledges]
ADD CONSTRAINT [PK_Priviledges]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [Index_Id] in table 'Goods'
ALTER TABLE [dbo].[Goods]
ADD CONSTRAINT [FK_IndexGood]
    FOREIGN KEY ([Index_Id])
    REFERENCES [dbo].[Indexes]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_IndexGood'
CREATE INDEX [IX_FK_IndexGood]
ON [dbo].[Goods]
    ([Index_Id]);
GO

-- Creating foreign key on [Good_Id] in table 'Quotations'
ALTER TABLE [dbo].[Quotations]
ADD CONSTRAINT [FK_GoodQuotation]
    FOREIGN KEY ([Good_Id])
    REFERENCES [dbo].[Goods]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_GoodQuotation'
CREATE INDEX [IX_FK_GoodQuotation]
ON [dbo].[Quotations]
    ([Good_Id]);
GO

-- Creating foreign key on [User_Id] in table 'Priviledges'
ALTER TABLE [dbo].[Priviledges]
ADD CONSTRAINT [FK_UserPriviledge]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserPriviledge'
CREATE INDEX [IX_FK_UserPriviledge]
ON [dbo].[Priviledges]
    ([User_Id]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------